/**
 * 
 */
package org.statewalker.graph.util;

import org.statewalker.graph.INodeWalkerListener;

/**
 * @author kotelnikov
 */
public class PrintNodeListener<T, E extends Exception>
    implements
    INodeWalkerListener<T, E> {

    private int fDepth;

    /**
     * 
     */
    public PrintNodeListener() {
    }

    public boolean beginNode(T parent, T node) throws E {
        printNode(node, "<", ">");
        fDepth++;
        return true;
    }

    public void endNode(T parent, T node) throws E {
        fDepth--;
        printNode(node, "</", ">");
    }

    protected String getName(T node) {
        return node.toString();
    }

    protected void print(String string) {
        System.out.print(string);
    }

    public void println(String string) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < fDepth; i++) {
            builder.append("    ");
        }
        builder.append(string);
        builder.append("\n");
        print(builder.toString());
    }

    protected void printNode(T node, String prefix, String suffix) {
        String name = getName(node);
        StringBuilder builder = new StringBuilder();
        builder.append(prefix);
        builder.append(name);
        builder.append(suffix);
        println(builder.toString());
    }

}
